﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using AnyOffice.Components;
using AnyOffice.DAL;
using FunkeLab;
using Microsoft.ApplicationBlocks.Data;
namespace AnyOffice.Control
{   
    public  partial  class ManageDepartmentControl : UserControl
    {
        private string _target = "main";
        private string _navigateUrl = "user.aspx";
        private SqlConnection conn;        
        public string NavigateUrl
        {
            get
            {
                return this._navigateUrl;
            }
            set
            {
                this._navigateUrl = value;
            }
        }

        public string Target
        {
            get
            {
                return this._target;
            }
            set
            {
                this._target = value;
            }
        }
       
        private void GetDept(ref MyTreeViewItem nodeItem, int deptId)
        {
            string commandText = "SELECT [DeptId],[ParentDept],[DeptName] FROM [dbo].[Department] WHERE [ParentDept]=" + deptId.ToString() + " ORDER BY [DeptNo] ASC";
            DataTable table = SqlHelper.ExecuteDataset(this.conn, CommandType.Text , commandText).Tables[0];
            if (table.Rows.Count > 0)
            {
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    MyTreeViewItem item = new MyTreeViewItem();
                    item.Text = table.Rows[i]["DeptName"].ToString();
                    item.Image = "/images/node_dept.gif";
                    this.LoadData(ref item, Convert.ToInt32(table.Rows[i]["DeptId"].ToString()));
                    this.GetDept(ref item, int.Parse(table.Rows[i]["DeptId"].ToString()));
                    nodeItem.Items.Add(item);
                }
            }
        }
       

        private void LoadData(ref MyTreeViewItem nodeDept, int deptId)
        {
            DataTable table = new UsersDao().SelectBaseByDeptId(this.conn, deptId);
            if (table.Rows.Count > 0)
            {
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    MyTreeViewItem item = new MyTreeViewItem();
                    item.Text = table.Rows[i]["UserName"].ToString();
                    item.Image = "/images/node_user.gif";
                    item.Link = this._navigateUrl + "?user_id=" + table.Rows[i]["UserId"].ToString() + "&user_name=" + Server.UrlEncode(table.Rows[i]["UserName"].ToString());
                    item.TargetFrame = this._target;
                    nodeDept.Items.Add(item);
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                DataTable table;
                MyTreeViewItem item;
                this.conn = new SqlConnection(ConfigManager.ConnectionString);
                ConfigManager manager = new ConfigManager();
                UserCache cache = new UserCache();
                bool flag = cache.PrivId == manager.AdministratorPrivId;
                string commandText = "SELECT [DeptId],[ParentDept],[DeptName] FROM [dbo].[Department] WHERE [DeptId] <> -1 ";
                if (flag)
                {
                    commandText = commandText + " AND [ParentDept] = 0 ORDER BY [DeptNo] ASC";
                    table = SqlHelper.ExecuteDataset(this.conn,  CommandType.Text, commandText).Tables[0];
                    if (table.Rows.Count > 0)
                    {
                        for (int i = 0; i < table.Rows.Count; i++)
                        {
                            item = new MyTreeViewItem();
                            item.Text = table.Rows[i]["DeptName"].ToString();
                            item.Image = "/images/node_dept.gif";
                            this.LoadData(ref item, Convert.ToInt32(table.Rows[i]["DeptId"].ToString()));
                            this.GetDept(ref item, int.Parse(table.Rows[i]["DeptId"].ToString()));
                            this.TreeView1.Items.Add(item);
                        }
                    }
                }
                else
                {
                    string postPriv = cache.PostPriv;
                    if (postPriv != null)
                    {
                        if (!(postPriv == "0"))
                        {
                            if (postPriv == "1")
                            {
                                commandText = commandText + " AND [ParentDept] = 0 ORDER BY [DeptNo] ASC";
                                table = SqlHelper.ExecuteDataset(this.conn, CommandType.Text, commandText).Tables[0];
                                if (table.Rows.Count > 0)
                                {
                                    for (int j = 0; j < table.Rows.Count; j++)
                                    {
                                        item = new MyTreeViewItem();
                                        item.Text = table.Rows[j]["DeptName"].ToString();
                                        item.Image = "/images/node_dept.gif";
                                        this.LoadData(ref item, Convert.ToInt32(table.Rows[j]["DeptId"].ToString()));
                                        this.GetDept(ref item, int.Parse(table.Rows[j]["DeptId"].ToString()));
                                        this.TreeView1.Items.Add(item);
                                    }
                                }
                            }
                            else if (postPriv == "2")
                            {
                                commandText = commandText + " AND CHARINDEX(','+ CAST([DeptId] AS nvarchar(20)) + ',','," + cache.PostDept + ",') > 0 ORDER BY [DeptNo] ASC";
                                table = SqlHelper.ExecuteDataset(this.conn, CommandType.Text, commandText).Tables[0];
                                if (table.Rows.Count > 0)
                                {
                                    for (int k = 0; k < table.Rows.Count; k++)
                                    {
                                        item = new MyTreeViewItem();
                                        item.Text = table.Rows[k]["DeptName"].ToString();
                                        item.Image = "/images/node_dept.gif";
                                        this.LoadData(ref item, Convert.ToInt32(table.Rows[k]["DeptId"].ToString()));
                                        this.TreeView1.Items.Add(item);
                                    }
                                }
                                else
                                {
                                    item = new MyTreeViewItem();
                                    item.Text = "无可管理部门";
                                    item.Image = "/images/endnode.gif";
                                    this.TreeView1.Items.Add(item);
                                }
                            }
                        }
                        else
                        {
                            commandText = commandText + " AND [DeptId] = " + cache.DeptId.ToString();
                            table = SqlHelper.ExecuteDataset(this.conn, CommandType.Text, commandText).Tables[0];
                            if (table.Rows.Count > 0)
                            {
                                item = new MyTreeViewItem();
                                item.Text = table.Rows[0]["DeptName"].ToString();
                                item.Image = "/images/node_dept.gif";
                                this.LoadData(ref item, Convert.ToInt32(table.Rows[0]["DeptId"].ToString()));
                                this.TreeView1.Items.Add(item);
                            }
                        }
                    }
                }
                this.conn.Close();
            }
        }


    }
}

